/******************************************************
 * 印刷进度
 * @return
 ****************************************************/

//添加明细按钮
function addSprkItem() {
	var xtId = $('#bbg_addWinTable input[qucln=XT_ID]').val();

	if (xtId == '' || xtId == null) {
		$.messager.alert('温馨提示', '亲，请先选择选题！');
		return;
	} else {
		_data_qw('ykj', 'ds_sprk_jh1', 'bbg_sure', '44', '_bbg_t_dg', '');
	}
}
// 回调函数
function _bbg_t_dg(prefix) {
	_zae_t_dg(prefix);
	$('#' + prefix + '_commDataLeftD a[id=' + prefix + '_dataQuery]').attr(
			'onclick', 'ysjdQuery(this)');
}
// 根据选题Id得到对应的商品Id
function getHIdByXtId(xtId) {
	var hId = '';
	$.ajax( {
		type : 'post',
		url : _m_url + 'sprk/getHIdByXtId',
		data : {
			'xtId' : xtId
		},
		async : false,
		success : function(data) {
			hId = data;
		}
	})
	return hId;
}
// 弹窗的查询方法
function ysjdQuery(obj) {
	var prefix = $(obj).attr('pre');
	var dsId = $(obj).attr('dsid');
	var resultDG = 'DATA_DATALIST';
	var comUseConf = 'commUseD';
	var hId = '';
	// 选题Id
	var xtId = $('#bbg_addWinTable input[qucln=XT_ID]').val();
	hId = getHIdByXtId(xtId);
	if (hId == '' || hId == null) {
		hId = 'null';
	}
	var otherConfig = "1-H_ID-" + hId + "-inc|+";
	var comUse = parseConfig(prefix + "_" + comUseConf);
	var useConf = otherConfig + comUse;
	$('#' + prefix + "_" + resultDG).datagrid( {
		url : _m_url + 'framework/loadDataQ',
		queryParams : {
			'dsId' : dsId,
			'config' : useConf
		}
	});
}
//确定按钮
function _bbg_sure() {
	$('#ykj_DATA_PARENT_PANEL').window('close');
	var selRows = $('#ykj_DATA_DATALIST').datagrid('getSelections');
	var oldRows = $('#bbg_addItemDatagrid').datagrid('getRows');
	var oldlength = oldRows.length;
	var totalAmount = 0;
//	if (oldlength == 0) {
		for ( var i = 0; i < selRows.length; i++) {
			$('#bbg_addItemDatagrid').datagrid('appendRow', {
				H_ID : selRows[i].H_ID,
				H_PRICE : selRows[i].H_PRICE,
				H_ISBN : selRows[i].H_ISBN,
				H_AMOUNT:selRows[i].H_AMOUNT,
				H_DISCOUNT:selRows[i].H_DISCOUNT,  // 折扣
				STOCK_ID:selRows[i].STOCK_ID,
				FX_JH_ITEM_ID:selRows[i].JH_ITEM_ID,
				STOCK_DATE:selRows[i].STOCK_DATE,
				MAN_ID:selRows[i].MAN_ID,
				H_TYPE:selRows[i].H_TYPE,
				YINCI:selRows[i].YINCI,
				BANYINCI:selRows[i].BANYINCI
			});
		}
//	} else {
//		for ( var j = 0; j < selRows.length; j++) {
//			for ( var k = 0; k < oldlength; k++) {
//				if (selRows[j].H_ISBN == oldRows[k].H_ISBN) {
//					break;
//				} else {
//					if (k == (oldlength - 1)) {
//						$('#bbg_addItemDatagrid').datagrid('appendRow', {
//							H_ID : selRows[j].H_ID,
//							H_PRICE : selRows[j].H_PRICE,
//							H_ISBN : selRows[j].H_ISBN,
//							H_AMOUNT:selRows[j].H_AMOUNT,
//							H_DISCOUNT:selRows[j].H_DISCOUNT,  // 折扣
//							STOCK_ID:selRows[j].STOCK_ID,
//							FX_JH_ITEM_ID:selRows[j].JH_ITEM_ID,
//							STOCK_DATE:selRows[j].STOCK_DATE,
//							MAN_ID:selRows[j].MAN_ID,
//							H_TYPE:selRows[j].H_TYPE
//						});
//					}
//				}
//			}
//		}
//	}
	var allRows =  $('#bbg_addItemDatagrid').datagrid('getRows');
	if(allRows.length ==0){
		totalAmount = 0;
	}else{
		for(var i =0;i<allRows.length;i++){
			if(allRows[i]["H_AMOUNT"] != null && allRows[i]["H_AMOUNT"] != ''){
				totalAmount = parseInt(totalAmount) + parseInt(allRows[i]["H_AMOUNT"]);
			}
		}
	}
	$('#bbg_addWinForm').find('input[qucln=TOTAL_AMONUT]').val(totalAmount);
	$('#bbg_addWinForm').find('input[qucln=TOTAL_AMONUT]').attr('chan','T');
	$('#bbg_addWinForm').find('input[qucln=TOTAL_AMONUT]').next().val(totalAmount);
}
//明细删除按钮
function itemRowDelBbg(prefix){
	itemRowDel(prefix);
	var totalAmount = 0;
	var allRows =  $('#bbg_addItemDatagrid').datagrid('getRows');
	if(allRows.length == 0){
		totalAmount = 0;
	}else{
		for(var i =0;i<allRows.length;i++){
			if(allRows[i]["H_AMOUNT"] != null && allRows[i]["H_AMOUNT"] != ''){
				totalAmount = parseInt(totalAmount) + parseInt(allRows[i]["H_AMOUNT"]);
			}
		}
	}
	$('#bbg_addWinForm').find('input[qucln=TOTAL_AMONUT]').val(totalAmount);
	$('#bbg_addWinForm').find('input[qucln=TOTAL_AMONUT]').attr('chan','T');
	$('#bbg_addWinForm').find('input[qucln=TOTAL_AMONUT]').next().val(totalAmount); 
}